package serv

import (
	"fmt"
	"net/http"

	"github.com/gin-gonic/gin"
)

func AdminNodeIndexCtrl(c *gin.Context) {
	if c.Request.Method == "POST" {
		param := GetJsonData(c)

		var adminNodeArr []map[string]any
		where := "pid=0"

		if param["name"] != nil {
			where += " and name like '%%" + param["name"].(string) + "%%'"
		}
		if param["username"] != nil {
			where += " and username like '%%" + param["username"].(string) + "%%'"
		}

		dbEngine.Table("f_node").Select("id,title,type,path,status,icon,pid,sort").Where(where).OrderBy("sort asc").Find(&adminNodeArr)
		cnt, _ := dbEngine.Table("f_node").Where(where).Count()

		for index, v := range adminNodeArr {
			var tmpArr []map[string]any
			dbEngine.Table("f_node").Select("id,title,type,path,status,icon,pid,sort").Where("pid=?", v["id"]).OrderBy("sort asc").Find(&tmpArr)
			adminNodeArr[index]["children"] = tmpArr
			fmt.Println(index, v)
		}

		c.JSON(200, ApiList{
			Status: 200,
			Data: ApiDataListObj{
				Total:       cnt,
				PerPage:     0,
				CurrentPage: 0,
				LastPage:    0,
				Data:        adminNodeArr,
			},
		})
	} else {
		c.HTML(http.StatusOK, "admin-node-index.html", nil)
	}
}

func AdminNodeGetFieldListCtrl(c *gin.Context) {
	if c.Request.Method == "POST" {

		var adminNodeArr []map[string]any
		where := "pid=0"

		dbEngine.Table("f_node").Select("id, id as val,title as `key`").Where(where).OrderBy("sort asc").Find(&adminNodeArr)

		for index, v := range adminNodeArr {
			var tmpArr []map[string]any
			dbEngine.Table("f_node").Select("id as val,title as `key`").Where("pid=?", v["id"]).OrderBy("sort asc").Find(&tmpArr)
			if len(tmpArr) > 0 {
				adminNodeArr[index]["children"] = tmpArr
			} else {
				adminNodeArr[index]["children"] = nil
			}
			fmt.Println(index, v)
		}

		c.JSON(200, map[string]any{"status": 200, "data": adminNodeArr})
	} else {
		c.HTML(http.StatusOK, "admin-node-index.html", nil)
	}
}

func AdminNodeGetUpdateInfoCtrl(c *gin.Context) {
	param := GetJsonData(c)
	id := param["id"]

	nodeArr := map[string]any{}
	dbEngine.Table("f_node").Select("id,title,type,path,status,icon,pid,sort").Where("id=?", id).Get(&nodeArr)

	c.JSON(200, map[string]any{"status": 200, "data": nodeArr})
}

func AdminNodeUpdateCtrl(c *gin.Context) {
	param := GetJsonData(c)
	if param["id"] == nil {
		dbEngine.Table("f_node").Insert(param)
		c.JSON(200, map[string]any{"status": 200, "msg": "添加成功"})
	} else {
		dbEngine.Table("f_node").Where("id=?", param["id"]).Update(param)
		c.JSON(200, map[string]any{"status": 200, "msg": "更新成功"})
	}
}

func AdminNodeDeleteCtrl(c *gin.Context) {
	param := GetJsonData(c)

	if param["id"] != nil {
		dbEngine.Table("f_node").Where("id=?", param["id"]).Delete()
		c.JSON(200, map[string]any{"status": 200, "msg": "删除成功"})
	}
}

func AdminNodeUpdateExtCtrl(c *gin.Context) {
	param := GetJsonData(c)

	if param["id"] != nil {
		dbEngine.Table("f_node").Where("id=?", param["id"]).Update(map[string]any{"status": param["status"]})

		c.JSON(200, map[string]any{"status": 200, "msg": "更新成功"})
	} else {
		c.JSON(200, map[string]any{"status": 200, "msg": "ID出错"})
	}
}
